monthly US energy consumption (renewables)

us_renew <- read_csv(here::here("data", "renewables_cons_prod.csv")) %>% 
  clean_names()
renew_clean <- us_renew %>% 
  mutate(description = str_to_lower(description)) %>% 
  filter(str_detect(description, pattern = "consumption")) %>% 
  filter(!str_detect(description, pattern = "total"))

Convert ‘yyyymm’ column to a date

renew_date <-renew_clean %>% 
  mutate(yr_mo_day = lubridate::parse_date_time(yyyymm, "ym")) %>% 
  mutate(month_sep = yearmonth(yr_mo_day)) %>%  # just pull out year and month, class will be yearmonth and date
  mutate(value = as.numeric(value)) %>% 
  drop_na(month_sep, value)  # specify the column, otherwise any rows have a na will be deleted

# make a version wher I have the month & year in separate columns
renew_parsed <- renew_date %>% 
  mutate(month = month(yr_mo_day, label = TRUE)) %>%  # label --> write in text not numbers
  mutate(year = year(yr_mo_day))

look at it

renew_gg <- ggplot(data = renew_date, aes(x = month_sep, y = value))+  # or aes(group = description)
  geom_line(aes(color = description))+
  theme_minimal()


renew_gg

updating colors with paletteer palettes:

renew_gg +
  scale_color_paletteer_d("basetheme::brutal")  # package_name::palette name

Coerce renew_parsed to a tsibble

renew_ts <- as_tsibble(renew_parsed, key = description, index = month_sep) # index, the time column

let’s look at our ts data in a couple different ways:

renew_ts %>% autoplot(value)

renew_ts %>% gg_subseries(value) 

renew_ts %>% gg_season(value)

# make the season plot in ggplot
ggplot(data = renew_parsed, aes(x = month, y = value, group = year))+
  geom_line(aes(color = year))+
  facet_wrap(~description,
             ncol = 1,
             scales = "free",
             strip.position = "right") # great! change the facet proporties!

Just look at the hydeoelectric energy consumption

hydro_ts <- renew_ts %>% 
  filter(description == "hydroelectric power consumption")

hydro_ts %>% autoplot(value)

hydro_ts %>% gg_subseries(value)

hydro_ts %>% gg_season(value)

What if I want quariterly average consumption for hydro?

hydro_quarterly <- hydro_ts %>% 
  index_by(year_qu = ~(yearquarter(.))) %>% # great way to group, new_column = ~
  summarize(avg_consumption = mean(value))

head(hydro_quarterly)
## # A tsibble: 6 x 2 [1Q]
##   year_qu avg_consumption
##     <qtr>           <dbl>
## 1 1973 Q1            261.
## 2 1973 Q2            255.
## 3 1973 Q3            212.
## 4 1973 Q4            225.
## 5 1974 Q1            292.
## 6 1974 Q2            290.

Decompose that hydro_ts

dcmp <- hydro_ts %>% 
  model(STL(value ~ season(window = 5)))

components(dcmp) %>% autoplot()

hist(components(dcmp)$remainder)

Now lookat the ACF

hydro_ts %>% 
  feasts::ACF(value) %>% 
  autoplot()

# 12 months later have the most correlation

DANGER DANGER do more research before making assumption

hydro_model <- hydro_ts %>% 
  model(
    ARIMA(value),
    ETS(value)
  ) %>% 
  fabletools::forecast(h = "4 years")

hydro_model %>% autoplot(filter(hydro_ts, year(month_sep) > 2010)) + theme_minimal()

Make a world map!

world <- read_sf(dsn = here::here("data", "TM_WORLD_BORDERS_SIMPL-0.3-1"),
                 layer = "TM_WORLD_BORDERS_SIMPL-0.3")

mapview(world)